/*
 * Copyright 2008 Daniel Freitas
 *
 * This file is part of Army Creator.
 * 
 * Army Creator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Army Creator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Army Creator.  If not, see <http://www.gnu.org/licenses/>.
 */
package cheesr.application.examples;

import java.util.Arrays;
import java.util.List;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;

import cheesr.application.CheesrApplication;
import cheesr.application.ClockPage;
import cheesr.application.Index;
import cheesr.model.Cheese;

public class Repeaters extends WebPage {

    @SuppressWarnings("serial")
    public Repeaters() {
        RepeatingView repeatMe = new RepeatingView("repeatMe");
        add(repeatMe);

        for (int i = 0; i < 5; i++) {
            repeatMe.add(new Label(repeatMe.newChildId(), "Value " + i));
        }

        // Adds a menu created by a repeater
        MenuItem[] menuItems = new MenuItem[] { new MenuItem("Index page", Index.class),
                new MenuItem("Labels page", Labels.class), new MenuItem("Clock page", ClockPage.class) };
        List<MenuItem> menuItemsList = Arrays.asList(menuItems);

        RepeatingView menu = new RepeatingView("menu");
        add(menu);

        for (MenuItem item : menuItemsList) {
            WebMarkupContainer webMarkupContainer = new WebMarkupContainer(menu.newChildId());
            menu.add(webMarkupContainer);
            webMarkupContainer.add(new BookmarkablePageLink("menuLink", item.getDestination()).add(new Label("caption",
                    item.getCaption())));
        }

        LoadableDetachableModel cheesesModel = new LoadableDetachableModel() {

            @Override
            protected Object load() {
                return ((CheesrApplication) getApplication()).getCheeses();
            }

        };

        // Repeats using ListView
        add(new ListView("cheeses", cheesesModel) {
            @Override
            protected void populateItem(final ListItem item) {
                item.add(new Label("name"));
                item.add(new Link("delete") {
                    @Override
                    public void onClick() {
                        Cheese cheese = (Cheese) item.getModelObject();
                        ((CheesrApplication) getApplication()).removeCheese(cheese);
                    }
                });
            }

            @SuppressWarnings("unchecked")
            @Override
            protected IModel getListItemModel(IModel listViewModel, int index) {
                Cheese cheese = ((List<Cheese>) listViewModel.getObject()).get(index);
                return new CompoundPropertyModel(new Model(cheese));
            }
        });
    }
}